/*

    @1.碰撞检测
    @2.敌机、子弹、奖励的运动
    
*/


function isCrash(a,b){
    var al=a.x;
    var at=a.y;
    var ar=al+a.w;
    var ab=at+a.h;
    
    var bl=b.x;
    var bt=b.y;
    var br=bl+b.w;
    var bb=bt+b.h;
    if(al>br||bl>ar||at>bb||bt>ab){
        return false;
    }
    return true;
}

var bombNum=0;//用来控制炸弹的数量

function allMove(){
    //敌机运动🏃
    for(var i=0;i<enemys.length;i++){
        var e=enemys[i];
        if(!e.isBol&&!hero.isBol&&isCrash(hero,e)){
            hero.isBol=true;
            return;
        }
        
        if(e.isDelete){
            enemys.splice(i,1);
            i--;
            continue;
        }
        e.draw();
    }
    //子弹运动🏃
    for(var i=0;i<bullets.length;i++){
        var b=bullets[i];
        
        for(var j=0;j<enemys.length;j++){
            var e=enemys[j];
            if(!e.isBol&&isCrash(e,b)){
                e.hp-=b.hurt;
                b.isDelete=true;
                break;
            }
        }
        
        if(b.isDelete){
            bullets.splice(i,1);
            i--;
            continue;
        }
        b.draw();
    }
    //奖励运动🏃
    for(var i=0;i<props.length;i++){
        var p=props[i];
        
        //奖励与英雄的碰撞
        if (isCrash(hero,p)){
            p.isDelete=true;
            if(p.type==0){
                //炸弹
                bombNum++;
                if(bombNum>=3){
                    bombNum=3;
                }
            }else if(p.type==1){
                //双排
                getDoubleBull();
            }else {
                //密集
                getCrowed();
            }
        }
        
        if(p.isDelete){
            props.splice(i,1);
            i--;
            continue;
        }
        p.draw();
    }
    
    //炸弹
    for(var i=1;i<=bombNum;i++){
        ctx.beginPath();
        ctx.drawImage(boomImg,(i-1)*42,canvas.height-36,42,36);
    }
    
    //结束
    if(hero.isOver){
        clearInterval(timer);
        clearInterval(doubleTimer);
        clearInterval(crowedTimer);
        timer=null;
        doubleTimer=null;
        crowedTimer=null;
        dt=0;
        ct=0;
        bombNum=0;
        enemys=[];
        bullets=[];
        props=[];
        div.style.display="block";
        if(score<30000){
            span.innerHTML="太菜了！！！好好练练吧";
        }else if(score<=60000&&score>=30000){
            span.innerHTML="还可以！！！下次继续努力";
        }else if(score<=140000&&score>60000){
            span.innerHTML="厉害了！！！做的不错";
        }else if(score>140000&&score<=200000){
            span.innerHTML="真的拽！！！干的漂亮";
        }else if(score>200000){
            span.innerHTML="无人可挡！！你----无敌了";
        }
        return;
    }
}
//密集时间到
var doubleTimer=null;
var dt=0;
function getDoubleBull(){
    dt+=8;
    isDouble=true;
    if(!doubleTimer){
        doubleTimer=setInterval(function(){
            dt--;
            if(dt==0){
                clearInterval(doubleTimer);
                doubleTimer=null;
                isDouble=false;
            }
        },1000)
    }
}

//双排时间到
var crowedTimer=null;
var ct=0;
function getCrowed(){
    ct+=8;
    isCrowed=true;
    if(!crowedTimer){
       crowedTimerr=setInterval(function(){
            ct--;
            if(ct==0){
                clearInterval(crowedTimer);
                crowedTimer=null;
                isCrowed=false;
            }
        },1000)
    }
}








